package org.hxzon.cyclone.pfgen;

import org.hxzon.cyclone.Particle;
import org.hxzon.cyclone.Vector3;
//pfgen.h,pfgen.cpp
//Bungee，蹦极
//绳索，只有拉力，没有张力

public class ParticleBungee implements ParticleForceGenerator {
    private Particle other;

    private double springConstant;

    private double restLength;

    public ParticleBungee(Particle other, double springConstant, double restLength) {
        this.other = other;
        this.springConstant = springConstant;
        this.restLength = restLength;
    }

    public void updateForce(Particle particle, double duration) {
        {
            Vector3 force = particle.getPosition().copy();
            force.minusUpdate(other.getPosition());

            double magnitude = force.magnitude();
            if (magnitude <= restLength) {
                return;
            }

            magnitude = springConstant * (restLength - magnitude);
            force.normalise();
            force.multiUpdate(-magnitude);
            particle.addForce(force);
        }
    }

}
